/******************************************************************************
*
*  Component
*  Originally part of the RadioPlayer hAxe project from haxe.org.
*  Updated 08.03.2007
*
*  Description:

   A simple class that create a MovieClip and provides easy manipulation and 
   extension.  I have, of course, made some major changes, including making
   the text element optional (and disabled by default), and allowing public 
   access via functions to many of the display variables.

******************************************************************************/

import flash.MovieClip;
import flash.TextField;
import flash.TextFormat;

class Component {

  public var x(getX,setX) : Float;
  public var y(getY,setY) : Float;
  public var width(getWidth,setWidth) : Float;
  public var height(getHeight,setHeight) : Float;
  public var text(getText,setText) : String;
  public var textcolor(getTextColor,setTextColor) : Int;
  public var color(getColor,setColor) : Int;
  public var alpha(getAlpha,setAlpha) : Int;
  public var visible(getVisible,setVisible) : Bool;
  private var g_hastext : Bool;

  var mc : MovieClip;
  var tf : TextField;
  var _width : Float;
  var _height : Float;
  var _color : Int;
  var _alpha : Int;

  public function new( root : MovieClip, ?createText:Bool, ?wordWrap:Bool, ?depth) {
    if (depth == null) {
      depth = root.getNextHighestDepth();
    }
    mc = root.createEmptyMovieClip("but"+depth,depth);
    g_hastext = createText;
    if (g_hastext) {
      mc.createTextField("tf",0,0,0,0,0);
      tf = Reflect.field(mc,"tf");
      tf.selectable = false;
      tf.wordWrap = wordWrap;
      var f = tf.getNewTextFormat();
      f.font = "Verdana,Arial,Helvetica";
      tf.setNewTextFormat(f);
    }
    _alpha = 100;
    _width = 0;
    _height = 0;
    _color = 0xFFFF00;
    mc._xscale = 100;
    mc._yscale = 100;
  }

  public function getVisible() {
    return mc._visible;
  }

  public function setVisible(b) {
    mc._visible = b;
    return b;
  }

  public function getX() {
    return mc._x;
  }

  public function getY() {
    return mc._y;
  }

  public function setX( x ) {
    mc._x = x;
    onMove();
    return x;
  }

  public function setY( y ) {
    mc._y = y;
    onMove();
    return y;
  }

  public function getWidth() {
    return _width;
  }

  public function getHeight() {
    return _height;
  }

  public function onMove() {
  }

  public function onResize() {
  }

  public function setWidth( w ) {
    _width = w;
    if (g_hastext) {
      tf._width = w;
    }
    onResize();
    redraw();
    return w;
  }

  public function setHeight( h ) {
    _height = h;
    if (g_hastext) {
      tf._height = h;
    }
    onResize();
    redraw();
    return h;
  }

  public function move (x, y, w, h) {
    mc._x = x;
    mc._y = y;
    _width = w;
    _height = h;
    if (g_hastext) {
      tf._width = w;
      tf._height = h;
    }
    onResize();
    redraw();
  }

  public function getText() {
    if (g_hastext) {
      return tf.text;
    } else {
      return "";
    }
  }

  public function setText(t) {
    if (g_hastext) {
      tf.text = t;
    }
    return t;
  }

  public function getTextColor() {
    if (g_hastext) {
      return tf.textColor;
    } else {
      return 0x0;
    }
  }

  public function setTextColor(c) {
    if (g_hastext) {
      tf.textColor = c;
    }
    return c;
  }

  public function getColor() {
    return _color;
  }

  public function setColor(c) {
    _color = c;
    redraw();
    return c;
  }

  public function getAlpha() {
    return _alpha;
  }

  public function setAlpha(a) {
    _alpha = a;
    redraw();
    return a;
  }

  public function getNewTextFormat () : TextFormat {
    if (g_hastext) {
      return tf.getNewTextFormat();
    }
    return null;
  }
  public function setNewTextFormat (f:TextFormat) : TextFormat {
    if (g_hastext) {
      tf.setNewTextFormat(f);
    }
    return f;
  }

  public function getTextFormat () : TextFormat {
    if (g_hastext) {
      return tf.getTextFormat();
    }
    return null;
  }
  public function setTextFormat (f:TextFormat) : TextFormat {
    if (g_hastext) {
      tf.setTextFormat(f);
    }
    return f;
  }

  public function getDepth () {
    return mc.getDepth();
  }

  function square(c,a,x1,y1,dx,dy) {    
    mc.moveTo(x1,y1);
    mc.beginFill(c,a);
    mc.lineTo(x1+dx,y1);
    mc.lineTo(x1+dx,y1+dy);
    mc.lineTo(x1,y1+dy);
    mc.lineTo(x1,y1);
    mc.endFill();
  }

  public function redraw() {
    mc.clear();
    square(_color,_alpha,0,0,width,height);
  }

  public function destroy() {
    mc.removeMovieClip();
  }

}